package org.example.servlet;

import org.example.service.UserService;
import org.example.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 注册用户
 */
public class RegServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		//设置请求响应编码格式
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");
		//获取请求信息
		String uname = req.getParameter("uname");
		String pwd = req.getParameter("pwd");
		String sex = req.getParameter("sex");
		int age =Integer.parseInt(req.getParameter("age")) ;
		String birthday = req.getParameter("birthday");
		//处理请求信息
		//获取业务层对象
		UserService us = new UserServiceImpl();
		 //处理注册
		int i=us.getUserInfoService(uname, pwd,sex,age,birthday);

		//相应处理结果
		//获取session对象
		HttpSession hs = req.getSession();
		//重定向到登陆界面
		if(i>0){
			//注册成功，添加标记到session
			hs.setAttribute("flag", "regsuccess");
			resp.sendRedirect("/login.jsp");
		}else{
			//重定向到注册页面
			resp.sendRedirect("/login.jsp");
		}
	}
}
